package com.aaa.book_mall.controller;

import com.aaa.book_mall.dao.BookDao;
import com.aaa.book_mall.dao.ProlnDao;
import com.aaa.book_mall.dao.RecordDao;
import com.aaa.book_mall.dao.impl.BookDaoImpl;
import com.aaa.book_mall.dao.impl.ProlnDaoImpl;
import com.aaa.book_mall.dao.impl.RecordDaoImpl;
import com.aaa.book_mall.entity.Book;
import com.aaa.book_mall.entity.Detail;
import com.aaa.book_mall.entity.Proln;
import com.aaa.book_mall.entity.Record;
import com.aaa.book_mall.util.LayTab;
import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 入库信息
 * fileName:ProlnServlet
 * description:
 * author:付亚星
 * createTime:2021/1/20 16:32
 * version:1.0.0
 */
@RestController
@RequestMapping("/proln")
public class ProlnController {
    private ProlnDao prolnDao = new ProlnDaoImpl();
    private RecordDao recordDao = new RecordDaoImpl();
    private BookDao bookDao = new BookDaoImpl();
    @RequestMapping("/showInfoProln")
    public LayTab showInfoProln(String ruku_number){
        //        查询对应的详情表
        List<Map<String, Object>> maps = prolnDao.showInfoProln(ruku_number);
        System.out.println("proln:::==="+maps);
        LayTab layTab = new LayTab(maps.size(), maps);
        return layTab;
    }

    /**
     * 添加入库成功时得操作
     * 向t_proln、t_rukurecord入库记录表、t_book表中添加数据
     */
    @RequestMapping("/add")
    public Map agree(@RequestBody ArrayList<Detail> Detaildata, HttpServletRequest request){
        System.out.println(Detaildata);
        //        获取现在的时间，和用户便函共同组成outnumber
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        String format = simpleDateFormat.format(date);
        String stringDate = 103 + format.replace("-", "").replace(":", "");
        System.out.println("outnumber" + stringDate);
        int bookcount = 0;
        double bookprice = 0;
        //      循环遍历采购的数据信息
        if(Detaildata.size() > 0){
            for (Detail Detail : Detaildata) {
                //proln信息表
                Proln proln = new Proln(null,stringDate,Detail.getBookid(),Detail.getBookcount());
              //  向proln信息表中加入数据
                int i = prolnDao.insertProln(proln);
                System.out.println("向proln表中添加数据成功！");
//                // 计算累计多少本书，累计多少钱
                bookcount = Detail.getBookcount();
            }
        }


        //入库记录表
        Record record = new Record(null,stringDate,101,"小楚",bookcount,bookprice,date);
        //       向t_rukurecord表中写数据
        int i = recordDao.insertRecord(record);

        /*       2、更改t_book对应书本的数量
         *             先根据number到t_proln表中查询申请的每一本书
         *             根据查询出来的书的量和书的编号到t_book表中操作
         * */
        Proln proln = new Proln();
        List<Proln> prolns = prolnDao.queryProln(proln.getRuku_number());
        if(prolns !=null && prolns.size()>0){
            for (Proln rukuproln : prolns) {
                Book book = new Book();

                int count = bookDao.selectCountByBookid(rukuproln.getBookid());
                count=bookcount+rukuproln.getBookcount();//将原有数量加上采购的数量
              //  bookDao.updateCountByBookid(count,rukuproln.getBookid());//更新t_book中的count

            }
        }

        HashMap<Object, Object> map = new HashMap<>();
        if (i > 0) {
            map.put("code", "1");
            map.put("msg", "添加成功");
        } else {
            map.put("code", "0");
            map.put("msg", "添加失败");
        }
        return map;
    }
    }
